<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h2>筛选搜索</h2>
      <el-form
        :inline="true"
        :model="queryParams"
        ref="queryForm"
        class="demo-form-inline"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="角色名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadRoles" :disabled="isLoading"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-button @click="handleDlg(0)">添加角色</el-button>
    <el-table
      :data="queryResult.records"
      border
      style="width: 100%"
      v-loading="isLoading"
    >
      <el-table-column type="index" label="序号" align="center">
      </el-table-column>
      <el-table-column prop="name" label="角色名称" align="center">
      </el-table-column>
      <el-table-column prop="description" label="描述" align="center">
      </el-table-column>
      <el-table-column
        prop="createdTime"
        label="添加时间"
        align="center"
        :formatter="timeFormater"
      >
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            @click="
              $router.push({
                name: 'alloc-menu',
                params: { roleId: scope.row.id },
              })
            "
            >分配菜单</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="
              $router.push({
                name: 'alloc-resource',
                params: { roleId: scope.row.id },
              })
            "
            >分配资源</el-button
          >
          <el-button size="mini" type="text" @click="handleDlg(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :disabled="isLoading"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.current"
      :page-sizes="[5, 10, 20]"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="queryResult.total"
    >
    </el-pagination>
    <role-create-or-edit
      ref="roleDlg"
      @role-change="loadRoles"
    ></role-create-or-edit>
  </el-card>
</template>
<script>
import { queryRoles, deleteRole } from '@/api/role'
import dayjs from 'dayjs'
import RoleCreateOrEdit from './create-or-edit.vue'

export default {
  name: 'RoleIndex',
  components: { RoleCreateOrEdit },
  data () {
    return {
      isLoading: false,
      queryParams: {
        code: undefined,
        id: undefined,
        name: '',
        current: 1,
        size: 5
      },
      queryResult: {}
    }
  },
  created () {
    this.loadRoles()
  },
  methods: {
    // 删除
    handleDelete (id) {
      this.$confirm('确认要删除角色吗？', '删除提示')
        .then(() => {
          // 如果点确定
          deleteRole(id).then((res) => {
            if (res.data.code === '000000') {
              this.$message.success('删除角色成功')
              this.loadRoles()
            } else {
              this.$message.error('删除角色失败')
            }
          })
        })
        .catch(() => {
          // 如果点取消
          this.$message.info('取消了删除角色的操作')
        })
    },
    handleDlg (id) {
      this.$refs.roleDlg.showAndInit(id)
    },
    timeFormater (row, column, value, index) {
      return dayjs(value).format('YYYY-MM-DD')
    },
    // 获取角色
    loadRoles () {
      this.isLoading = true
      queryRoles(this.queryParams)
        .then((res) => {
          if (res.data.code === '000000') {
            const { current, pages, records, size, total } = res.data.data
            Object.assign(this.queryParams, { current, size })
            this.queryResult = { pages, records, total }
          } else {
            this.$message.error('查询角色失败')
          }
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    // 重置
    handleReset () {
      this.$refs.queryForm.resetFields()
    },
    handleSizeChange (val) {
      this.queryParams.size = val
      this.queryParams.current = 1
      this.loadRoles()
    },
    handleCurrentChange (val) {
      this.queryParams.current = val
      this.loadRoles()
    }
  }
}
</script>
<style scoped lang="scss">
.el-form {
  margin-left: 100px;
}
.el-table {
  margin: 20px;
}
</style>
